package models;

import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.DomainIp;

public class DomainIpModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "ip_address_";
	
	private int limit = 100;
	
	private static DomainIpModel instance = null;
	
	public static DomainIpModel getInstance() {
		if (instance == null) {
			instance = new DomainIpModel();
		}
		
		return instance;
	}
	
	private DomainIpModel() {
		try {
			ObjectifyService.register(DomainIp.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public void insert(DomainIp domainIp) {
		if (cache != null) {
			 cache.put(prefix + domainIp.getIp(), domainIp);
		}
		ofy.put(domainIp);
	}
	
	public DomainIp get(String ip) {
		DomainIp domainIp = new DomainIp();
		
		if (cache != null && cache.containsKey(prefix + ip)) {
			domainIp = (DomainIp) cache.get(prefix + ip);
		} else {
			try {
				domainIp = ofy.get(new Key<DomainIp>(DomainIp.class, ip));
				cache.put(prefix + ip, domainIp);
			} catch (Exception e) {
				domainIp = null;
			}
		}
		return domainIp;
	}
	
	public DomainIp add(String ip, List<String> links) {
		boolean isChange = false;
		DomainIp domainIp = get(ip);
		if (domainIp == null) {
			domainIp = new DomainIp();
			domainIp.setIp(ip);
		}
		
		List<String> domains = domainIp.getDomains();
		for (int i = 0; i < links.size(); i++) {
			if (!domains.contains(links.get(i)) && domains.size() <= limit) {
				isChange = true;
				domains.add(links.get(i));
			}
		}
		
		domainIp.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				 cache.put(prefix + ip, domainIp);
			}
			insert(domainIp);
		}
		
		return domainIp;
	}
	
	public DomainIp insert(String ip, String domain) {
		boolean isChange = false;
		DomainIp domainIp = get(ip);
		if (domainIp == null) {
			domainIp = new DomainIp();
			domainIp.setIp(ip);
		}
		
		List<String> domains = domainIp.getDomains();
		if (!domains.contains(domain) && domains.size() <= limit) {
			isChange = true;
			domains.add(domain);
		}
		
		domainIp.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				 cache.put(prefix + ip, domainIp);
			}
			insert(domainIp);
		}
		
		return domainIp;
	}
	
	public void delete(DomainIp domainIp) {
		if (cache != null && cache.containsKey(prefix + domainIp.getIp())) {
			cache.remove(prefix + domainIp.getIp());
		}
		ofy.delete(domainIp);
	}
}
